package com.cjjd.c.feature.mine.register;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.MobileCode;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.CountDownTimerUtils;
import com.cjjd.c.utils.FragmentUtil;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.StringUtil;
import com.cjjd.c.utils.ToastUtil;
import com.cjjd.c.view.MyEditText;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/10.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class RegisterFragment extends ActionBarFragment {
    @Bind(R.id.et_mobile)
    MyEditText etMobile;
    @Bind(R.id.mtv_captcha)
    MyEditText mtvCaptcha;
    @Bind(R.id.tv_get_captcha)
    TextView tvGetCaptcha;

    private String mobile;
    private String sessionId = "";
    private String captcha = "";

    @Override
    public Object getTitleString() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_register;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_get_captcha, R.id.tv_next,R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_get_captcha:
                if (checkMobile()) {
                    getRegMobileCode();
                }
                break;
            case R.id.tv_next:
                checkCaptcha();
                break;
            case R.id.back:
                mContext.finish();
                break;
        }
    }

    private void checkCaptcha() {

        mobile = etMobile.getText().toString().trim();
        captcha = mtvCaptcha.getText().toString().trim();

        if (TextUtils.isEmpty(captcha)) {
            ToastUtil.showLongToast(mContext, "请输入手机验证码！");
            return;
        }


        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);
        h1.put("captcha", captcha);
        h1.put("sessionId", sessionId);

        //{"message":"成功","return_code":"0"}
        Call<HttpResult<String>> checkRegMobileCode = service.checkRegMobileCode(JsonUtils.convertParam("checkRegMobileCode", h1));
        checkRegMobileCode.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {

                SetPsdFragment setPsdFragment = new SetPsdFragment();
                setPsdFragment.setData(mobile,captcha,sessionId);

                FragmentUtil.replaceWithoutAddToBack(mContext, setPsdFragment, R.id.fragment_container);
            }

        });

    }


    private void getRegMobileCode() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);

        LogUtil.e("xyd", "formatString = " + JsonUtils.convertParam("getRegMobileCode", h1));

        // TODO: 2017/8/24  这里不行，接口返回数据不规范，没法封装
        // {"captcha":"949038","message":"成功","return_code":"0","sessionId":"370ce886-2f46-4062-9ff4-9bfa8616af72"}
        Call<HttpResult<MobileCode>> call = service.getRegMobileCode(JsonUtils.convertParam("getRegMobileCode", h1));
        call.enqueue(new CallbackImpl<MobileCode>() {
            @Override
            public void responseSuccess(Call<HttpResult<MobileCode>> call, Response<HttpResult<MobileCode>> response) {

                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCaptcha, 60000, 1000);
                countDownTimerUtils.setMillisColor(getResources().getColor(R.color.white));
                countDownTimerUtils.start();

                MobileCode regMobileCode = response.body().getData();
                sessionId = regMobileCode.sessionId;
            }
        });
    }


    private boolean checkMobile() {
        mobile = etMobile.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showLongToast(mContext, "请输入手机号码！");
            return false;
        }
        if (!StringUtil.isMobile(mobile)) {
            ToastUtil.showLongToast(mContext, "请输入正确的手机号码！");
            return false;
        }
        return true;
    }
}
